﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DB.EF
{
    public class GenericRepository<T> where T : class
    {
        protected DbContext Context;

        public GenericRepository(DbContext context)
        {
            this.Context = context;
        }

        public virtual T Add(T entity)
        {
            Context.Set<T>().Add(entity);
            Context.SaveChanges();
            return entity;
        }

        public virtual T Delete(T entity)
        {
            Context.Set<T>().Remove(entity);
            Context.SaveChanges();
            return entity;
        }

        public virtual T Update(T entity)
        {
            var entry = Context.Entry(entity);
            entry.State = EntityState.Modified;
            Context.SaveChanges();
            return entity;
        }
    }
}
